<?php

class my_cart extends a_front_controller {

    protected $srv;
    protected $cart;

    public function __construct() {
        parent::__construct(true);
        $this->srv = new service_l_cart();
        $this->cart = $this->srv->get_cart();
    }

    public function index() {
        
    }

    public function clear() {
        $this->cart->remove_all();
    }

    public function update() {
        if (count($post = $this->input->post('cart_product'))) {
            foreach ($post as $pid => $num_qty) {
                try {
                    $this->cart->update_item($pid, array_shift($num_qty));
                } catch (invalid_qty_exception_l_cart $iqe) {
                    $this->add_global_msg($iqe->getMessage());
                } catch (product_not_found_exception_l_cart $pnf) {
                    $this->add_global_msg($pnf->getMessage());
                } catch (not_in_cart_exception_l_cart $nic) {
                    $this->add_global_msg($nic->getMessage());
                } catch (Exception $ex) {
                    $this->add_global_msg($ex->getMessage());
                }
            }
        }
    }

    public function remove($id) {
        try {
            $this->cart->remove_item($id);
        } catch (not_in_cart_exception_l_cart $nic) {
            //@TO_DO : log??
        } catch (Exception $exc) {
            //@TO_DO : log??
        }
    }

    public function checkout() {
        redirect("checkout_details/process_details");
    }

    public function batch_assign() {
        $this->assign("cart_items", $this->cart->get_all_items());
    }

}

?>